<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\SoftDeletes;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property int $uid 私域用户ID
 * @property int $main_uid 主账号ID
 * @property string $name 标签名称
 * @property int $module 标签模块
 * @property int $parent_id 上级ID
 * @property int $sort 排序
 * @property int $sub_type 子类型：0=社群行为 1=客户行为
 * @property int $duration 间隔
 * @property int $unit 单位: 0=天 1=小时 2=分站 3=秒钟
 * @property int $is_preset 是否预设标签：0=否 1=是
 * @property int $contact_cnt 联系人数量
 * @property int $created_by 创建者
 * @property int $updated_by 更新者
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $remark 备注
 * @property string $deleted_at 删除时间
 * @property-read null|\Hyperf\Database\Model\Collection|ChSmartTag[] $tags 
 */
class ChSmartTag extends MineModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_smart_tag';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'uid', 'main_uid', 'name', 'module', 'parent_id', 'sort', 'sub_type', 'duration', 'unit', 'is_preset', 'contact_cnt', 'created_by', 'updated_by', 'created_at', 'updated_at', 'remark', 'deleted_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'main_uid' => 'integer', 'module' => 'integer', 'parent_id' => 'integer', 'sort' => 'integer', 'sub_type' => 'integer', 'duration' => 'integer', 'unit' => 'integer', 'is_preset' => 'integer', 'contact_cnt' => 'integer', 'created_by' => 'integer', 'updated_by' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function tags(): \Hyperf\Database\Model\Relations\HasMany
    {
        return $this->hasMany(ChSmartTag::class, 'parent_id', 'id');
    }

    /**
     * 客户
     * @return BelongsToMany
     */
    public function contacts(): BelongsToMany
    {
        return $this->belongsToMany(ChContact::class, 'ch_contact_smart_tag', 'smart_tag_id', 'contact_id');
    }
}
